package com.net.httpTest;

import java.io.IOException;
import java.net.*;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
public class Client {
    public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {
        HttpClient httpClient = HttpClient.newBuilder()
                .version(HttpClient.Version.HTTP_1_1)
                .followRedirects(HttpClient.Redirect.NEVER)
                .connectTimeout(Duration.ofSeconds(10))
                .proxy(ProxySelector.getDefault())//正向代理服务器配置，为空不配置
                .build();

        HttpRequest request = HttpRequest.newBuilder()
                .uri(new URI("https://v2.xxapi.cn/api/weather?city=北京"))
//                .setHeader("content-Type", "applicaton-json")
                .GET()
                .build();

        HttpResponse<String> send = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
        HttpResponse<String> send2 = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(send.body());
        System.out.println(send2.body());


    }
}
